var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js")
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
const message = '是否确认取消该订单？'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderstate:0,
    pagesize:6,
    pageIndex: 1,
    scrollTop: 0,
    scrollHeight:0,
    recordList:[],
    hasData:false,
    loading: false,
    netWork: false,
    isLoading: true,
    visible:false,
    itemData:[],
    currentTab:0,
    tabs: ['巡检记录', '清理记录'],
    tabs_state: [0,1],
    check_state:-1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
    this.data.check_state =  parseInt(options.check_state)
    this.setData({
      scrollHeight: app.globalData.windowHeight + 4,
    })
  },
  onChange:function(event) {
    console.log('onChange',event)
    var index = event.detail.index
    var state = this.data.tabs_state[index]
    this.data.orderstate = state
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      recordList:[]
    });

    this.onHttpInit(state)
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function (is_clean) {
    var that = this
    fetchApi.httpPost('/terminal/recordlist', {
      is_clean: is_clean,
      pagesize: that.data.pagesize,
      pageindex: that.data.pageIndex,
      check_state: that.data.check_state
    }, function (res) {
      console.log("订单列表数据", res)
      if (res.data.code == 0) {
        that.data.pageIndex = that.data.pageIndex + 1
        if (res.data.totalcount > 0) {
          var records = res.data.data
          console.log('records',records)
          for (var index in records) {
            that.data.recordList.splice(that.data.recordList.length, 0, records[index]);
          }
          that.setData({
            hasData: true,
            recordList: that.data.recordList
          })
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  preImg: function (t) {
    console.log("preSunImg", t)
    var a = t.currentTarget.dataset.imageindex, e = t.currentTarget.dataset.imgdata || [];
    var c = [];
    for (var index in e) {
      c.push(e[index].imgUrl)
    }
    common_Js.preImg(a, c)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onHttpInit(this.data.orderstate)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshrecordList()
  },
  refreshrecordList:function(){
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      recordList:[]
    })
    this.data.pageIndex = 1
    this.onHttpInit(this.data.orderstate)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit(this.data.orderstate)
  },
})